/**********************************************************************
 *** 名称：GameSystem.cpp
 *** 描述：GameSystem命名空间中方法的定义。
 **********************************************************************/
#include "GameSystem.h"

namespace GameSystem{
	void initSystem(){

		putenv("SDL_VIDEO_WINDOW_POS");
		putenv("SDL_VIDEO_CENTERED=1");

		/* 初始化视频、音频系统 */
		if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0){
			std::cout<<"Error when initialize SDL: "<<SDL_GetError()<<std::endl;
			exit(1);
		}

		/* 初始化Mixer的格式支持 */
		int initMixFlag = MIX_INIT_MP3;
		if((Mix_Init(initMixFlag) & initMixFlag) != initMixFlag){
			std::cout<<"Error when initialize SDL_Mixer: "<<SDL_GetError()<<std::endl;
			exit(1);
		}

		TTF_Init();
	}

	void systemQuit(){
		Mix_Quit();
		SDL_Quit();
	}

  /* 中文编码转换 */
	char *localToUTF8(std::string src){
	    const int MAX_LEN = 521;
        size_t src_len = strlen(src.c_str());
        char dst[MAX_LEN];
        size_t dst_len = sizeof(dst);
        memset(dst,0,MAX_LEN);
        char *in = (char *)src.c_str();
        char *out = dst;

        iconv_t cd = iconv_open("utf-8", "gb2312");
        if(cd == (iconv_t) - 1){
            std::cout<<"Can NOT open the iconv for translation."<<std::endl;
            exit(0);
        }
        if(iconv(cd, (const char **)&in, &src_len, &out, &dst_len) == -1){
            std::cout<<"Can NOT translation."<<std::endl;
            exit(1);
        }
        iconv_close(cd);
		return dst;
	}

	char *localToUTF8(char *src){
	    const int MAX_LEN = 521;
        size_t src_len = strlen(src);
        char dst[MAX_LEN];
        size_t dst_len = sizeof(dst);
        memset(dst,0,MAX_LEN);
        char *in = src;
        char *out = dst;

        iconv_t cd = iconv_open("utf-8", "gb2312");
        if(cd == (iconv_t) - 1){
            std::cout<<"Can NOT open the iconv for translation."<<std::endl;
            exit(0);
        }
        if(iconv(cd, (const char **)&in, &src_len, &out, &dst_len) == -1){
            std::cout<<"Can NOT translation."<<std::endl;
            exit(1);
        }
        iconv_close(cd);
		return dst;
	}
}
